package org.jc.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jc.subject.application.convert.SubjectInfoDTOConverter;
import org.jc.subject.application.dto.SubjectInfoDTO;
import org.jc.subject.common.entity.PageResult;
import org.jc.subject.common.entity.Result;
import org.jc.subject.domain.entity.SubjectInfoBO;
import org.jc.subject.domain.service.SubjectDomainService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 功能描述
 *
 * @author wanghongqian
 * @date 2024/06/13 15:55
 */

@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectDomainService subjectDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),"题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"题目分数不能为空");

            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),"分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()),"标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            subjectDomainService.add(subjectInfoBO);
            return Result.ok(true);
        }catch (Exception e){
            log.error("SubjectController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }

    }

    @PostMapping("/getSubject/page")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectController.getSubjectPage.dto:{}",JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            PageResult<SubjectInfoBO> subjectInfoDTOPageResult = subjectDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(subjectInfoDTOPageResult);
        }catch (Exception e){
            log.error("SubjectController.getSubjectPage.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            SubjectInfoBO boResult = subjectDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO dtoResult = SubjectInfoDTOConverter.INSTANCE.convertBOToDTO(boResult);
            return Result.ok(dtoResult);
        }catch (Exception e){
            log.error("SubjectController.querySubjectInfo.error:{}",e.getMessage(),e);
            return Result.fail("查询题目详细信息失败");
        }
    }
}
